/*
 * Copyright (C) 2010 Yury Kudryashov.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package name.kudryashov.collections;

import name.kudryashov.annotations.Immutable;
import name.kudryashov.base.Argument;
import name.kudryashov.functions.Function2;

import java.io.Serializable;
import java.util.Arrays;
import java.util.Iterator;

/**
 * Tuple of two elements.
 * @param <T1> type of first element
 * @param <T2> type of second element
 * <p>
 * Implements {@link Iterable} allowing to iterate over tuple's members as over objects.
 */
@Immutable
public class Tuple2<T1, T2> implements Serializable, Iterable<Object> {

    private static final long serialVersionUID = -215524156000721265L;

    private final T1 first;
    private final T2 second;

    public Tuple2(T1 first, T2 second) {
        this.first = first;
        this.second = second;
    }

    /**
     * @return first member of the tuple
     */
    public T1 get1() {
        return first;
    }

    /**
     * @return second member of the tuple
     */
    public T2 get2() {
        return second;
    }

    public Iterator<Object> iterator() {
        return Arrays.<Object>asList(first, second).iterator();
    }

    /**
     * Reduces both tuple's members to a single value applying the given function.
     * @param f function, not null
     * @param <R> type of result
     * @return the result of calling the given function with tuple's members as arguments
     * @throws NullPointerException f is null
     */
    public <R> R reduce(Function2<T1, T2, R> f) {
        return Argument.requireNotNull(f, "f").apply(first, second);
    }

    /**
     * @return a new {@link Tuple2} with members swapped.
     */
    @SuppressWarnings({"unchecked"})
    public Tuple2<T2, T1> swap() {
        return new Tuple2<T2,T1>(second, first);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Tuple2 tuple2 = (Tuple2) o;

        return !(first != null ? !first.equals(tuple2.first) : tuple2.first != null) &&
               !(second != null ? !second.equals(tuple2.second) : tuple2.second != null);
    }

    @Override
    public int hashCode() {
        int result = first != null ? first.hashCode() : 0;
        result = 31 * result + (second != null ? second.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "(" + first + ", " + second + ")";
    }
}